package com.derbysoft.ota.application.translator.rs;

import java.math.BigInteger;

import com.derbysoft.modules.eip.translator.Translator;
import com.derbysoft.ota.application.util.Calendars;
import com.derbysoft.ota.interfaces.protocol._2009a.RateType;
import com.derbysoft.ota.interfaces.protocol._2009a.RateType.Rate;
import com.derbysoft.ota.interfaces.protocol._2009a.RoomStayType.RoomRates.RoomRate;
import com.derbysoft.remote.dswitch.hotel.dto.RateDTO;
import com.derbysoft.remote.dswitch.hotel.dto.ReservationRoomStayDTO;
import com.derbysoft.remote.dswitch.hotel.dto.RoomRateDTO;
import com.google.common.base.Function;
import com.google.inject.Inject;
import com.googlecode.functionalcollections.FunctionalIterables;
import org.apache.commons.collections.CollectionUtils;

import javax.annotation.Nullable;

public class ResRoomRateTranslator implements Translator<ReservationRoomStayDTO, RoomRate> {

	private final Translator<RateDTO, Rate> rateTranslator;

	@Inject
	public ResRoomRateTranslator(Translator<RateDTO, Rate> rateTranslator) {
		this.rateTranslator = rateTranslator;
	}

	public RoomRate translate(ReservationRoomStayDTO source) {
		RoomRate roomRate = new RoomRate();
		roomRate.setNumberOfUnits(BigInteger.valueOf(source.getNumberOfUnits()));
		roomRate.setRoomTypeCode(source.getRoomTypeCode());
		roomRate.setRatePlanCode(source.getRatePlanCode());
		roomRate.setEffectiveDate(Calendars.of(source.getStayDateRange().getCheckInDate()));
		roomRate.setExpireDate(Calendars.of(source.getStayDateRange().getCheckOutDate()));
		roomRate.setExpireDateExclusiveIndicator(Boolean.TRUE);
		RoomRateDTO roomRateDTO = source.getRoomRate();
        if (roomRateDTO != null
                && CollectionUtils.isNotEmpty(roomRateDTO.getRates())) {
            roomRate.setRates(new RateType());
            roomRate.getRates().getRate().addAll(FunctionalIterables.make(roomRateDTO.getRates()).map(
					new Function<RateDTO, Rate>() {
						public Rate apply(@Nullable RateDTO from) {
							return rateTranslator.translate(from);
						}
					}
			).toList());
		}
		return roomRate;
	}

}
